package mj

import (
	"common/utils"
	"game/component/mj/alg"
	"game/component/mj/mp"
	"sync"
)

type Logic struct {
	sync.RWMutex
	cards    []mp.CardID
	gameType GameType
	qidui    bool
	huLogic  *alg.HuLogic
}

func (l *Logic) washCards() {
	l.Lock()
	defer l.Unlock()
	l.cards = []mp.CardID{
		mp.Wan1, mp.Wan2, mp.Wan3, mp.Wan4, mp.Wan5, mp.Wan6, mp.Wan7, mp.Wan8, mp.Wan9,
		mp.Wan1, mp.Wan2, mp.Wan3, mp.Wan4, mp.Wan5, mp.Wan6, mp.Wan7, mp.Wan8, mp.Wan9,
		mp.Wan1, mp.Wan2, mp.Wan3, mp.Wan4, mp.Wan5, mp.Wan6, mp.Wan7, mp.Wan8, mp.Wan9,
		mp.Wan1, mp.Wan2, mp.Wan3, mp.Wan4, mp.Wan5, mp.Wan6, mp.Wan7, mp.Wan8, mp.Wan9,
		mp.Tong1, mp.Tong2, mp.Tong3, mp.Tong4, mp.Tong5, mp.Tong6, mp.Tong7, mp.Tong8, mp.Tong9,
		mp.Tong1, mp.Tong2, mp.Tong3, mp.Tong4, mp.Tong5, mp.Tong6, mp.Tong7, mp.Tong8, mp.Tong9,
		mp.Tong1, mp.Tong2, mp.Tong3, mp.Tong4, mp.Tong5, mp.Tong6, mp.Tong7, mp.Tong8, mp.Tong9,
		mp.Tong1, mp.Tong2, mp.Tong3, mp.Tong4, mp.Tong5, mp.Tong6, mp.Tong7, mp.Tong8, mp.Tong9,
		mp.Tiao1, mp.Tiao2, mp.Tiao3, mp.Tiao4, mp.Tiao5, mp.Tiao6, mp.Tiao7, mp.Tiao8, mp.Tiao9,
		mp.Tiao1, mp.Tiao2, mp.Tiao3, mp.Tiao4, mp.Tiao5, mp.Tiao6, mp.Tiao7, mp.Tiao8, mp.Tiao9,
		mp.Tiao1, mp.Tiao2, mp.Tiao3, mp.Tiao4, mp.Tiao5, mp.Tiao6, mp.Tiao7, mp.Tiao8, mp.Tiao9,
		mp.Tiao1, mp.Tiao2, mp.Tiao3, mp.Tiao4, mp.Tiao5, mp.Tiao6, mp.Tiao7, mp.Tiao8, mp.Tiao9,
		mp.Zhong, mp.Zhong, mp.Zhong, mp.Zhong,
	}
	if l.gameType == HongZhong8 {
		l.cards = append(l.cards, mp.Zhong, mp.Zhong, mp.Zhong, mp.Zhong)
	}
	for i := 0; i < 300; i++ {
		index := i % len(l.cards)
		random := utils.Rand(len(l.cards))
		l.cards[index], l.cards[random] = l.cards[random], l.cards[index]
	}
}

func (l *Logic) getCards(num int) []mp.CardID {
	// 发牌之后 牌就没了
	l.Lock()
	defer l.Unlock()
	if len(l.cards) < num {
		return nil
	}
	cards := l.cards[:num]
	l.cards = l.cards[num:]
	return cards
}

func (l *Logic) getRestCardsCount() int {
	return len(l.cards)
}

func (l *Logic) canHu(cards []mp.CardID, card mp.CardID) bool {
	// 胡牌判断 复杂的一套逻辑
	return l.huLogic.CheckHu(cards, []mp.CardID{mp.Zhong}, card)
}

func (l *Logic) getOperateArray(cards []mp.CardID, card mp.CardID) []OperateType {
	operateArray := make([]OperateType, 0)
	if card < 0 && card > 35 {
		return operateArray
	}
	// 判断碰 已经有两个相同的 如果card 和这个一直 能凑成 三个一样的
	sameCount := 0
	for _, v := range cards {
		if v == card {
			sameCount++
		}
	}
	if sameCount >= 2 {
		operateArray = append(operateArray, Peng)
	}
	if sameCount >= 3 {
		operateArray = append(operateArray, GangChi)
	}
	if l.canHu(cards, card) {
		operateArray = append(operateArray, HuChi)
	}
	if len(operateArray) > 0 {
		operateArray = append(operateArray, Guo)
	}
	return operateArray
}

func (l *Logic) getRestCards() []mp.CardID {
	return l.cards
}

func (l *Logic) getCard(card mp.CardID) mp.CardID {
	indexOf := alg.IndexOf(l.cards, card)
	if indexOf == -1 {
		return 0
	}
	l.cards = append(l.cards[:indexOf], l.cards[indexOf+1:]...)
	return card
}

func NewLogic(gameType GameType, qidui bool) *Logic {
	return &Logic{
		gameType: gameType,
		qidui:    qidui,
		huLogic:  alg.NewHuLogic(),
	}
}
